package com.cango.cmdb.mobielcmdb;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebChromeClient;
import android.view.KeyEvent;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 实例化
        mWebView = (WebView) findViewById(R.id.mWebView);
        // 开启localstorage
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        mWebView.setLayerType(mWebView.LAYER_TYPE_HARDWARE,null);
        // 载入内容
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        // 开启javascript 渲染
        mWebView.getSettings().setJavaScriptEnabled(true);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mWebView.loadUrl("file:///android_asset/index.html");
            }
        });

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack(); //goBack()表示返回WebView的上一页面
            return true;
        }
        return false;

    }
}
